/*
 * 灯光预设管理页面视图
 * @Author: LuoQianJun
 * @Date: 2018-03-31 14:52:07
 * @Last Modified by: ljb
 * @Last Modified time: 2019-08-27 10:31:02
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-equipment-views {
        padding: 20px;
    }

</style>
<template>

	<div class="v-admin-equipment-views">

		<Tabs
			v-model="tag_name"
			type="card"
			@on-click="changeTab"
		>
			<!-- @on-click="changeTab -->
			<!-- 新增 -->
			<TabPane
				:label="$t('equipment.equipment')"
				name="新增">
				<equipment-create
					ref="create"
					:loading="table_loading"
					:language_index="language_index"
					:manufactures="manufactures"
					@postStore="postStoreData"
				/>
			</TabPane>

			<!-- 查看 -->
			<TabPane
				:label="$t('equipment.index')"
				name="查看">
				<banner-index
					:device_index="device_index"
					:loading="table_loading"
					@edit="bannerDetail"
					@delete="deleteBanner"
					@sort="confirmSearch"
				/>

				<br>

				<!-- 分页组件 -->
				<Row
					type="flex"
					justify="center">
					<Page
						:total="manufacturer_index_total"
						:current="page_info.page"
						:page-size="page_info.per_page"
						placement="top"
						show-sizer
						show-total
						@on-change="pageChange"
						@on-page-size-change="perPageChange"
					/>
				</Row>
			</TabPane>


		</tabs>
		<Modal
			v-model="show_modal"
			:title="$t('lightingprese.update_lightingprese')"
			class-name="vertical-middle-modal"
			@on-cancel="hide_BannerModal">

			<!-- 编辑组件 -->
			<manufacturer-edit
				v-if="show_modal"
				ref="edit"
				:language_index="language_index"
				:manufactures="manufactures"
				:manufacturer_solo="manufacturer_solo"
				:loading="button_loading"
				@update="updateBanner"/>

			<div slot="footer"/>

		</Modal>

	</div>

</template>

<script>

import API_LIST from 'src/API_LIST';
import { mapMutations } from 'vuex';
import qs from 'qs';
import { deleteEmptyValue } from 'src/utils/utils';
// import purchaseIndex from './purchase/purchase-index';
import equipmentCreate from './create';
import bannerIndex from './index';
import manufacturerEdit from './edit';

export default {

	name: 'EquipmentViews',

	components: {
		// purchaseIndex,
		equipmentCreate,
		bannerIndex,
		manufacturerEdit,

	},
	data() {
		return {
			table_loading: false, // 表格更新时的loading

			show_modal: false, // 弹框的控制

			show_editModal: false, // 机器设备编辑弹窗的控制

			show_deviceModal: false, // 账号列表信息弹窗的控制

			show_editDeviceModal: false, // 修改账户信息弹窗的控制

			button_loading: false, // 按钮的loading

			tag_name: '新增',

			device_index: [], // 设备列表index

			language_index: [], // 语言列表

			manufacturer_solo: {},

			manufactures: [], // 可选择的生产商下拉框选项

			manufacturer_index_total: 0, // index列表條數


			page_info: {
				// 分页信息
				page: 1,
				per_page: 20,

			},

			search_info: {

				page: 1,
				per_page: 20,
			},

		};
	},
	mounted() {
		this.fetchEquipmentCategory();
		this.Common_langua();
	},
	methods: {

		...mapMutations('spinShow', [
			'showSpin',
			'closeSpin',
		]),

		hide_BannerModal() {
			this.manufacturer_solo = null;
		},

         		/**
         * 获取新增入库分类信息
         * @return {undefined}
         */
		fetchEquipmentCategory() {
			this.showSpin();

			this.$http.get(`${API_LIST.LIGHTING_PRESET_MANAGEMENT}/create`)

				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);
					console.log(data);

					this.manufactures = data.devices;
				})

				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})

				.then(this.closeSpin);
		},

		bannerDetail(detail) {
			this.manufacturer_solo = detail;
			this.show_modal = true;
		},

		/**
         * 删除Banner
         * @param  {Number} id {需要删除的banner的id}
         * @return {undefined}
         */
		deleteBanner(id) {
			this.$Modal.confirm({

				title: this.$t('product.tip'), // '温馨提示',
				content: this.$t('product.is_delete'), // '删除之后将无法恢复，确定吗？',
				onOk: () => {
					const param = {

						_method: 'DELETE',

					};
					this.table_loading = true;

					this.$http.post(`${API_LIST.LIGHTING_PRESET_MANAGEMENT}/${id}`, param)

						.then(({ data }) => {
							if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

							this.fetchDeviceList(this.search_info);

							this.$Message.success(this.$t('common.delete_success'));
						})

						.catch((err) => {
							this.$Message.error(err.message || err.statusText);
						})

						.then(() => { this.table_loading = false; });
				},
				onCancel: () => {},

			});
		},

		/**
       * 页码改变
       * @param  {string} page {当前页码}
       * @return {undefined}
       */
		pageChange(page) {
			this.search_info.page = page;
			this.search_info.per_page = this.page_info.per_page;
			this.fetchDeviceList(this.search_info);
		},

		/**
       * 每页显示数改变
       * @param  {string} size {当前每页显示数}
       * @return {undefined}
       */
		perPageChange(size) {
			// this.page_info.page = size;
			this.search_info.per_page = size;
			this.page_info.per_page = size;
			this.fetchDeviceList(this.page_info);
		},

		/* 排序 */
		confirmSearch({
			keyword, sort_column, sort_by,
		}) {
			const param = {
				page: this.page_info.page,

				per_page: this.page_info.per_page,

				name: keyword,
			};

			switch (sort_by) {
			case '':

				break;
			default:
				param.order_bys = [
					{
						sort: sort_by,
						order_by: sort_column,
					},
				];
			}

			this.search_info = JSON.parse(JSON.stringify(param));

			this.fetchDeviceList(this.search_info);
		},


		/**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined}
         */
		changeTab(tab_name) {
	       		if (tab_name === '查看') {
				this.fetchDeviceList(this.search_info);

				this.$refs.create.handleReset('formValidate');
			}
		},

		updateBanner(id, param) {
			this.showSpin();

			this.$http.post(`${API_LIST.LIGHTING_PRESET_MANAGEMENT}/${id}`, param)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.update_success'));

					this.show_modal = false;

					this.fetchDeviceList(this.search_info);
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},


		/**
         * 新增
		 * @param  {Object} param {post请求所需的参数对象}
         * @return {undefined}
         */
		postStoreData(param) {
			this.showSpin(this.$t('common.please_wait'));


			this.$http.post(API_LIST.LIGHTING_PRESET_MANAGEMENT, param)

				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.create_success'));

					this.$refs.create.handleReset('formValidate');

					this.fetchEquipmentCategory();
				})

				.catch((err) => {
					this.$Message.error(err.message);
					console.log(err);
				})

				.then(this.closeSpin);
		},


		// 请求语言列表
		Common_langua() {
			this.$http.get(`${API_LIST.COMMON_LANGUAGE}`)
				.then(({ data }) => {
					if (parseInt(data.code) !== 0) throw new Error(data.error);
					this.language_index = data.languages;
				})
				.catch((err) => {
					this.$Message.error(err.message);
				});
		},
		fetchDeviceList(param) {
			this.table_loading = true;

			const query_str = qs.stringify(param);

			this.$http.get(`${API_LIST.LIGHTING_PRESET_MANAGEMENT}?${query_str}`)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);
					console.log(data);

					this.device_index = data.device_presets.data;

					this.manufacturer_index_total = data.device_presets.total;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},
	},
};
</script>
